package com.itheima.studentproject.dao;

import com.itheima.studentproject.domain.Student;

public class StudentDao extends BaseStudentDao{
    private static Student[] arr = new Student[5];

    static {
        Student stu1 = new Student("heima001","张三","23","1999-01-01");
        Student stu2 = new Student("heima002","李四","24","2000-01-01");

        arr[0] = stu1;
        arr[1] = stu2;
    }

    @Override
    public boolean addStudent(Student student){
        boolean flag = false;

        for (int i = 0; i < arr.length; i++) {
            Student stu1 = arr[i];
            if(stu1 == null){
                arr[i] = student;
                flag = true;
                break;
            }
        }

        return flag;
    }
    @Override
    public Student[] findAll() {
        return arr;
    }
    @Override
    public boolean updateStudent(Student stu) {
        for (int i = 0; i < arr.length; i++) {
            Student student = arr[i];
            if (student != null) {
                if(stu.getId().equals(student.getId())){
                    arr[i] = stu;
                    return true;
                }
            }
        }
        return false;
    }
    @Override
    public boolean deleteStudent(int index) {
        arr[index] = null;
        return true;
    }
    @Override
    public int isExist(String id) {
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(id.equals(stu.getId())){
                index = i;
                break;

            }
        }
        return index;
    }
}
